package com.meirengu.webview.service.impl;

import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.webview.common.ConfigConsts;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.KwCyphFilter;
import com.meirengu.webview.model.KwInterfaceConfig;
import com.meirengu.webview.model.User;
import com.meirengu.webview.model.UserInfo;
import com.meirengu.webview.service.*;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.meirengu.utils.DateUtils.*;
import static com.meirengu.webview.common.ConfigConsts.CHANNEL_CODE_KAISEN2;
import static com.meirengu.webview.common.ConfigConsts.CHANNEL_NAME_KAISEN2;
import static com.meirengu.webview.common.Constants.BUSINESS_STATUS_SUCCESS;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
import static java.util.Calendar.*;
import static org.apache.commons.lang3.StringUtils.isEmpty;

/**
 * KwCyphFilter服务实现层
 *
 * @author Mao
 * @create Tue May 08 11:08:03 CST 2018
 */
@Service
public class KwCyphFilterServiceImpl extends BaseServiceImpl<KwCyphFilter> implements KwCyphFilterService {


    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private KwCyphPushService kwCyphPushService;

    @Autowired
    private KwHnbPushService kwHnbPushService;

    @Autowired
    private KwYcdPushService kwYcdPushService;

    @Autowired
    private KwXbbFilterService kwXbbFilterService;


    Logger logger = LoggerFactory.getLogger(KwCyphFilterServiceImpl.class);


    /**
     * 发送畅愿普惠接口
     * 过滤条件
     * 1 全国
     * 2 23到55
     * 3 房车寿3其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendChangYuanpuhui(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            //校验是否有黑牛发送成功的记录
            Map<String, Object> cyphparams = new HashMap<>();
            cyphparams.put("mobile", user.getMobile());
            cyphparams.put("code", 1);
            Integer count = kwHnbPushService.getCount(cyphparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count)) {
                return sendFlag;
            }
            //校验是否有一财道普惠发送成功的记录
            Map<String, Object> ycdparams = new HashMap<>();
            ycdparams.put("mobile", user.getMobile());
            ycdparams.put("interfaceCode", "ycdph");
            ycdparams.put("code", 1);
            Integer ycdCount = kwYcdPushService.getCount(ycdparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(ycdCount)) {
                return sendFlag;
            }
            userInfo.setCreateDate(new Date());
            String cyphIol = redisClient.get("iol-cyph");
            if ("N".equals(cyphIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                String ip = userInfo.getIp();

                Integer income = userInfo.getSalary();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer fund = userInfo.getFund();

                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);

                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(mobile);
                kwCyphFilter.setChannelCode("cyph");
                kwCyphFilter.setChannelName("畅愿普惠");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("cyph");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getCarValue().contains(car.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {

                            String sexValue = "M";
                            if (sex.intValue() == 1) {
                                sexValue = "F";
                            }
                            String birthValue = dateToStr(brithdayDate, "yyyy-MM-dd");
                            String cityValue = city.replace("市", "");
                            String incomeStr = "10000以上";
                            //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                            if (income.intValue() == 1) {
                                incomeStr = "3000以下";
                            } else if (income.intValue() == 2) {
                                incomeStr = "3000-5000";
                            } else if (income.intValue() == 3) {
                                incomeStr = "5001-8000";
                            } else if (income.intValue() == 4) {
                                incomeStr = "8001-10000";
                            }
                            String hasCreditCard = "yes";
                            //信用情况 1无信用卡无贷款   2有信用卡或贷款
                            if (credit.intValue() == 1) {
                                hasCreditCard = "no";
                            }
                            String hasHouse = "yes";
                            String hasHousingLoan = "yes";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 1) {
                                hasHouse = "no";
                                hasHousingLoan = "no";
                            } else if (house.intValue() == 3) {
                                hasHousingLoan = "no";
                            }
                            String hasCar = "yes";
                            String hasCarLoan = "yes";
                            //名下车   1 无车   2 有车贷    3有车无贷
                            if (car.intValue() == 1) {
                                hasCar = "no";
                                hasCarLoan = "no";
                            } else if (car.intValue() == 3) {
                                hasCarLoan = "no";
                            }
                            String hasLifeInsurance = "yes";
                            String insurancePay = "2400";
                            //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                            if (lifeInsurance.intValue() == 1) {
                                hasLifeInsurance = "no";
                                insurancePay = "0";
                            } else if (lifeInsurance.intValue() == 2) {
                                insurancePay = "2000";
                            }
                            //社保   0无社保   1一年以内    2一年以上
                            //住房公积金   0无公积金   1一年以内    2一年以上
                            String applyTime = dateToStr(userInfo.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
                            Integer code = kwCyphPushService.applyLoan(name, mobile, ip, "WAP",
                                    sexValue, birthValue, cityValue, incomeStr, expectAmount.intValue() + "",
                                    age + "", hasCreditCard, hasHouse, hasHousingLoan,
                                    hasCar, hasCarLoan, hasLifeInsurance, insurancePay,
                                    socialSecurity.intValue() + "", fund.intValue() + "", applyTime);
                            kwCyphFilter.setSuited("Y");
                            kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "cyph", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("用户无房：" + house.intValue() + "  用户无车：" + car.intValue() + "  用户无寿险：" + lifeInsurance.intValue());
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    /**
     * 发送畅愿保险接口
     * 限制年龄  25-50
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendChangYuanInsure(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {

            String cyphIol = redisClient.get("iol-cyis");
            if ("N".equals(cyphIol)) {
                userInfo.setCreateDate(new Date());
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                String ip = userInfo.getIp();

                Integer income = userInfo.getSalary();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();

                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);

                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(mobile);
                kwCyphFilter.setChannelCode("cyis");
                kwCyphFilter.setChannelName("畅愿保险");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("cyis");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }
                    if (dataFlag) {

                        String sexValue = "M";
                        if (sex.intValue() == 1) {
                            sexValue = "F";
                        }
                        String birthValue = dateToStr(brithdayDate, "yyyy-MM-dd");
                        String cityValue = city.replace("市", "");
                        String incomeStr = "10000以上";
                        //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                        if (income.intValue() == 1) {
                            incomeStr = "3000以下";
                        } else if (income.intValue() == 2) {
                            incomeStr = "3000-5000";
                        } else if (income.intValue() == 3) {
                            incomeStr = "5001-8000";
                        } else if (income.intValue() == 4) {
                            incomeStr = "8001-10000";
                        }
                        String hasHouse = "Y";
                        //名下房产  1 无房   2 有房贷   3有房无贷
                        if (house.intValue() == 1) {
                            hasHouse = "N";
                        }
                        String hasCar = "Y";
                        //名下车   1 无车   2 有车贷    3有车无贷
                        if (car.intValue() == 1) {
                            hasCar = "N";
                        }
                        String hasLifeInsurance = "Y";
                        //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                        if (lifeInsurance.intValue() == 1) {
                            hasLifeInsurance = "N";
                        }
                        //社保   0无社保   1一年以内    2一年以上
                        //住房公积金   0无公积金   1一年以内    2一年以上
                        String applyTime = dateToStr(userInfo.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
                        Integer code = kwCyphPushService.applyInsurance(name, mobile, ip, sexValue,
                                birthValue, cityValue, incomeStr, hasCar, hasHouse, hasLifeInsurance, applyTime, user.getUserAgent());
                        kwCyphFilter.setSuited("Y");
                        kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "cyis", user);
                        if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        /*畅愿失败投新旦*/
        if (!sendFlag) {
            kwXbbFilterService.sendXbb(userInfo, user);
        }
        return sendFlag;
    }

    /**
     * 发送畅愿普惠接口2
     * 过滤条件
     * 1 全国
     * 2 23到55
     * 3 房车寿3其一
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */

    public boolean sendChangYuanpuhui2(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            //校验是否有畅愿发送成功的记录
            Map<String, Object> cyphparams = new HashMap<>();
            cyphparams.put("mobile", user.getMobile());
            cyphparams.put("interfaceCode", "cyph");
            cyphparams.put("code", 1);
            Integer count = kwCyphPushService.getCount(cyphparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count)) {
                return sendFlag;
            }
            //校验是否有黑牛发送成功的记录
            Map<String, Object> hnbparams = new HashMap<>();
            hnbparams.put("mobile", user.getMobile());
            hnbparams.put("code", 1);
            Integer hnbCount = kwHnbPushService.getCount(hnbparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(hnbCount)) {
                return sendFlag;
            }
            //校验是否有一财道普惠发送成功的记录
            Map<String, Object> ycdparams = new HashMap<>();
            ycdparams.put("mobile", user.getMobile());
            ycdparams.put("interfaceCode", "ycdph");
            ycdparams.put("code", 1);
            Integer ycdCount = kwYcdPushService.getCount(ycdparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(ycdCount)) {
                return sendFlag;
            }
            userInfo.setCreateDate(new Date());
            String cyph2Iol = redisClient.get("iol-cyph2");
            if ("N".equals(cyph2Iol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                String ip = userInfo.getIp();

                Integer income = userInfo.getSalary();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer fund = userInfo.getFund();

                int age = 25;
                Date brithdayDate = userInfo.getBirthday();
                age = DateUtils.getAge(brithdayDate);

                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(mobile);
                kwCyphFilter.setChannelCode("cyph2");
                kwCyphFilter.setChannelName("畅愿普惠2");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("cyph2");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }
                    if (dataFlag) {
                        if (kwInterfaceConfig.getHouseValue().contains(house.intValue() + "") ||
                                kwInterfaceConfig.getCarValue().contains(car.intValue() + "") ||
                                kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {

                            String sexValue = "M";
                            if (sex.intValue() == 1) {
                                sexValue = "F";
                            }
                            String birthValue = dateToStr(brithdayDate, "yyyy-MM-dd");
                            String cityValue = city.replace("市", "");
                            String incomeStr = "10000以上";
                            //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                            if (income.intValue() == 1) {
                                incomeStr = "3000以下";
                            } else if (income.intValue() == 2) {
                                incomeStr = "3000-5000";
                            } else if (income.intValue() == 3) {
                                incomeStr = "5001-8000";
                            } else if (income.intValue() == 4) {
                                incomeStr = "8001-10000";
                            }
                            String hasCreditCard = "yes";
                            //信用情况 1无信用卡无贷款   2有信用卡或贷款
                            if (credit.intValue() == 1) {
                                hasCreditCard = "no";
                            }
                            String hasHouse = "yes";
                            String hasHousingLoan = "yes";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 1) {
                                hasHouse = "no";
                                hasHousingLoan = "no";
                            } else if (house.intValue() == 3) {
                                hasHousingLoan = "no";
                            }
                            String hasCar = "yes";
                            String hasCarLoan = "yes";
                            //名下车   1 无车   2 有车贷    3有车无贷
                            if (car.intValue() == 1) {
                                hasCar = "no";
                                hasCarLoan = "no";
                            } else if (car.intValue() == 3) {
                                hasCarLoan = "no";
                            }
                            String hasLifeInsurance = "yes";
                            String insurancePay = "2400";
                            //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                            if (lifeInsurance.intValue() == 1) {
                                hasLifeInsurance = "no";
                                insurancePay = "0";
                            } else if (lifeInsurance.intValue() == 2) {
                                insurancePay = "2000";
                            }
                            //社保   0无社保   1一年以内    2一年以上
                            //住房公积金   0无公积金   1一年以内    2一年以上
                            String applyTime = dateToStr(userInfo.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
                            Integer code = kwCyphPushService.applyLoan2(name, mobile, ip, "WAP",
                                    sexValue, birthValue, cityValue, incomeStr, expectAmount.intValue() + "",
                                    age + "", hasCreditCard, hasHouse, hasHousingLoan,
                                    hasCar, hasCarLoan, hasLifeInsurance, insurancePay,
                                    socialSecurity.intValue() + "", fund.intValue() + "", applyTime);
                            kwCyphFilter.setSuited("Y");
                            kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "cyph2", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("用户无房：" + house.intValue() + "  用户无车：" + car.intValue() + "  用户无寿险：" + lifeInsurance.intValue());
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }


    /**
     * 发送畅愿阳光接口
     * 过滤条件
     * 1 指定城市
     * 3 必须有信用卡或贷款
     *
     * @param userInfo
     * @param user
     * @throws Exception
     */
    public boolean sendChangyuanSun(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            //校验是否有畅愿发送成功的记录  畅愿
            Map<String, Object> cyphparams = new HashMap<>();
            cyphparams.put("mobile", user.getMobile());
            cyphparams.put("interfaceCode", "cyph");
            Integer count = kwCyphPushService.getCount(cyphparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count)) {
                return sendFlag;
            }

            //校验是否有畅愿发送成功的记录  畅愿2
            Map<String, Object> cyph2params = new HashMap<>();
            cyph2params.put("mobile", user.getMobile());
            cyph2params.put("interfaceCode", "cyph2");
            Integer count2 = kwCyphPushService.getCount(cyph2params);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(count2)) {
                return sendFlag;
            }

            userInfo.setCreateDate(new Date());
            String cyygIol = redisClient.get("iol-cyyg");
            if ("N".equals(cyygIol)) {
                String id = user.getId();
                String name = user.getName();
                String phone = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                Integer income = userInfo.getSalary();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer loan = expectAmount.intValue();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer fund = userInfo.getFund();
                Integer salaryMethod = userInfo.getSalaryMethod();
                String age = "25";
                Date brithdayDate = userInfo.getBirthday();
                Integer ageCount = DateUtils.getAge(brithdayDate);
                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(phone);
                kwCyphFilter.setChannelCode("cyyg");
                kwCyphFilter.setChannelName("畅愿阳光");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("cyyg");

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (ageCount < kwInterfaceConfig.getMinAge().intValue() || ageCount > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }
                    if (income.intValue() < 4) {
                        dataFlag = false;
                        kwCyphFilter.setSuited("N");
                        kwCyphFilter.setCode(UserChannelEnum.FAIL_SALARY.getValue());
                        kwCyphFilter.setRefuseMsg("收入不足10000：" + income.intValue());
                    }
                    if (dataFlag) {
                        if (credit.intValue() == 2) {
                            age = ageCount.toString();
                            String loanRecords = "Y";
                            String hasloan = "Y";
                            String gender = "1";
                            if (sex.intValue() == 1) {
                                gender = "2";
                            }
                            String birthday = dateToStr(brithdayDate, "yyyy-MM-dd");
                            String salary = "15000";
                            //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                            if (income.intValue() == 1) {
                                salary = "3000";
                            } else if (income.intValue() == 2) {
                                salary = "5000";
                            } else if (income.intValue() == 3) {
                                salary = "8000";
                            } else if (income.intValue() == 4) {
                                salary = "10000";
                            }
                            String hasCreditCard = "Y";
                            //信用情况 1无信用卡无贷款   2有信用卡或贷款
                            if (credit.intValue() == 1) {
                                hasCreditCard = "N";
                            }
                            String hasHouse = "Y";
                            String hasHouseLoan = "Y";
                            //名下房产  1 无房   2 有房贷   3有房无贷
                            if (house.intValue() == 1) {
                                hasHouse = "N";
                                hasHouseLoan = "N";
                            } else if (house.intValue() == 3) {
                                hasHouseLoan = "N";
                            }
                            String hasCar = "Y";
                            String hasCarLoan = "Y";
                            //名下车   1 无车   2 有车贷    3有车无贷
                            if (car.intValue() == 1) {
                                hasCar = "N";
                                hasCarLoan = "N";
                            } else if (car.intValue() == 3) {
                                hasCarLoan = "N";
                            }
                            String hasInsurance = "Y";
                            //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                            if (lifeInsurance.intValue() == 1) {
                                hasInsurance = "N";
                            }
                            //社保   0无社保   1一年以内    2一年以上
                            String hasSocialSecurity = "Y";
                            if (socialSecurity.intValue() == 0) {
                                hasSocialSecurity = "N";
                            }
                            //住房公积金   0无公积金   1一年以内    2一年以上
                            String hasHouseFund = "Y";
                            if (fund.intValue() == 0) {
                                hasHouseFund = "N";
                            }
                            String salaryType = "1";
                            if (salaryMethod.intValue() == 2) {
                                salaryType = "2";
                            }

                            String applyTime = dateToStr(userInfo.getCreateDate(), "yyyy-MM-dd");
                            Integer code = kwCyphPushService.applyLoanSun(name, phone, gender, age, birthday, applyTime, salary, salaryType,
                                    loanRecords, hasCreditCard, hasHouse, hasHouseLoan, hasHouseFund, hasCar, hasCarLoan, hasSocialSecurity, hasInsurance,
                                    hasloan, loan, city);
                            kwCyphFilter.setSuited("Y");
                            kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                            /*推送接口后修改限制数量*/
                            userInfoService.updateLimitNum(code, "cyyg", user);
                            if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                                sendFlag = true;
                            }
                        } else {
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("用户信用卡或贷款" + credit.intValue());
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    private BigDecimal[] bigDecimals = new BigDecimal[]{new BigDecimal(50000), new BigDecimal(60000)
            , new BigDecimal(70000), new BigDecimal(80000), new BigDecimal(90000), new BigDecimal(100000)
            , new BigDecimal(110000), new BigDecimal(120000), new BigDecimal(130000), new BigDecimal(140000)
            , new BigDecimal(150000)};

    private Random random = new Random();

    @Override
    public boolean sendKaisen(UserInfo userInfo, User user) {
        boolean sendFlag = false;
        try {
            String cyygIol = redisClient.get("iol-" + ConfigConsts.CHANNEL_CODE_KAISEN);
            if ("N".equals(cyygIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String userAgent = user.getUserAgent();
                String city = userInfo.getScity();
                String ip = userInfo.getIp();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                Date brithdayDate = userInfo.getBirthday();
                String idCard = userInfo.getIdCard();
                Integer age = DateUtils.getAge(brithdayDate);
                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(mobile);
                kwCyphFilter.setChannelCode(ConfigConsts.CHANNEL_CODE_KAISEN);
                kwCyphFilter.setChannelName(ConfigConsts.CHANNEL_NAME_KAISEN);

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode(ConfigConsts.CHANNEL_CODE_KAISEN);

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }

                    //身份证不能为空
                    if (isEmpty(idCard)) {
                        dataFlag = false;
                        kwCyphFilter.setSuited("N");
                        kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                        kwCyphFilter.setRefuseMsg("没有身份证");
                    }

                    if (dataFlag) {
                        String gender = "男";
                        if (sex != null && sex == 1) {
                            gender = "女";
                        }

                        String birthday = dateToStr(brithdayDate, "yyyy-MM-dd");

                        /*
                        StringBuilder sb = new StringBuilder();

                        if (car != null && (car == 2 || car == 3)) {
                            sb.append("车有/");
                        } else {
                            sb.append("车无/");
                        }

                        if (house != null && (house == 2 || house == 3)) {
                            sb.append("房有/");
                        } else {
                            sb.append("房无/");
                        }

                        if (lifeInsurance != null && (lifeInsurance == 2 || lifeInsurance == 3)) {
                            sb.append("保单有/");
                        } else {
                            sb.append("保单无/");
                        }

                        if (credit != null && credit == 1) {
                            sb.append("信用卡有");
                        } else {
                            sb.append("信用卡无");
                        }
                        */

                        expectAmount = bigDecimals[random.nextInt(11)];

                        StringBuilder remark1Sb = new StringBuilder();
                        remark1Sb.append("贷款").append("/");
                        if (credit != null && credit == 1) {
                            remark1Sb.append("信用卡有");
                        } else {
                            remark1Sb.append("信用卡无");
                        }
                        remark1Sb.append("/").append(expectAmount.toString());

                        String dateStr = dateToStr(userInfo.getCreateDate());

                        Integer code = kwCyphPushService.applyKaisen(name, idCard, mobile, city, gender, birthday, "", remark1Sb.toString(), ""
                                , ip, "", userAgent, "", dateStr, "");
                        kwCyphFilter.setSuited("Y");
                        kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, ConfigConsts.CHANNEL_CODE_KAISEN, user);
                        if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendKaisen2(UserInfo userInfo, User user) {
        boolean sendFlag = false;
        try {
            String cyygIol = redisClient.get("iol-" + CHANNEL_CODE_KAISEN2);
            if ("N".equals(cyygIol)) {
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String userAgent = user.getUserAgent();
                String city = userInfo.getScity();
                Integer areaCode = userInfo.getAreaCode();
                Integer salary = userInfo.getSalary();
                Integer salaryMethod = userInfo.getSalaryMethod();
                String ip = userInfo.getIp();
                Integer credit = userInfo.getCredit();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer house = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer fund = userInfo.getFund();
                Integer lifeInsurance = userInfo.getPolicy();
                Integer wxloan = userInfo.getWxloan();
                Date brithdayDate = userInfo.getBirthday();
                String idCard = userInfo.getIdCard();
                Integer age = DateUtils.getAge(brithdayDate);
                KwCyphFilter kwCyphFilter = new KwCyphFilter();
                kwCyphFilter.setUserId(id);
                kwCyphFilter.setMobile(mobile);
                kwCyphFilter.setChannelCode(CHANNEL_CODE_KAISEN2);
                kwCyphFilter.setChannelName(CHANNEL_NAME_KAISEN2);

                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode(CHANNEL_CODE_KAISEN2);

                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwCyphFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwCyphFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwCyphFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitHouse())) {
                        if (!kwInterfaceConfig.getHouseValue().contains(house.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有房：" + house.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitCar())) {
                        if (!kwInterfaceConfig.getCarValue().contains(car.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有车：" + car.intValue());
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitPolicy())) {
                        if (!kwInterfaceConfig.getPolicyValue().contains(lifeInsurance.intValue() + "")) {
                            dataFlag = false;
                            kwCyphFilter.setSuited("N");
                            kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                            kwCyphFilter.setRefuseMsg("没有寿险：" + lifeInsurance.intValue());
                        }
                    }

                    //身份证不能为空
/*
                    if (isEmpty(idCard)) {
                        dataFlag = false;
                        kwCyphFilter.setSuited("N");
                        kwCyphFilter.setCode(UserChannelEnum.FAIL_O2O.getValue());
                        kwCyphFilter.setRefuseMsg("没有身份证");
                    }
*/

                    if (dataFlag) {
                        String extId = createExtId();

                        String cityCode = "";
                        if (areaCode != null) {
                            cityCode = String.valueOf(areaCode);
                        }

                        String gender = "男";
                        if (sex != null && sex == 1) {
                            gender = "女";
                        }

                        String birthday = dateToStr(brithdayDate, "yyyy-MM-dd");

                        String income = "5000";
                        if (salary == null) {
                        } else if (salary == 1) {
                            income = "3000";
                        } else if (salary == 2) {
                            income = "5000";
                        } else if (salary == 3) {
                            income = "8000";
                        } else if (salary == 4) {
                            income = "10000";
                        } else if (salary == 5) {
                            income = "12000";
                        }

                        String wagesType = "银行转账";
                        if (salaryMethod != null && salaryMethod == 2) {
                            wagesType = "现金发放";
                        }

                        String room = "无";
                        if (house != null && (house == 2 || house == 3)) {
                            room = "有";
                        }

                        String carStr = "无";
                        if (car != null && (car == 2 || car == 3)) {
                            carStr = "有";
                        }

                        String socialSecurityStr = "无";
                        if (socialSecurity != null && (socialSecurity == 1 || socialSecurity == 2)) {
                            socialSecurityStr = "有";
                        }

                        String accumulationFund = "无";
                        if (fund != null && (fund == 1 || fund == 2)) {
                            accumulationFund = "有";
                        }

                        String creditStr = "无";
                        if (credit != null && credit == 1) {
                            creditStr = "有";
                        }

                        String policy = "无";
                        if (lifeInsurance != null && (lifeInsurance == 2 || lifeInsurance == 3)) {
                            policy = "有";
                        }

                        String wld = "无";
                        if (wxloan != null && wxloan == 1) {
                            wld = "有";
                        }

                        String createTime = dateFormat.format(new Date());

                        Integer code = kwCyphPushService.applyKaisen2(extId, name, idCard, mobile, "tdb", city, cityCode
                                , gender, String.valueOf(age), birthday, "", income, wagesType, expectAmount.toBigInteger().toString()
                                , room, carStr, socialSecurityStr, accumulationFund, creditStr, policy, wld, ip, ""
                                , createTime, "贷款赠险", "", userAgent, "", "", "", "KS2");

                        kwCyphFilter.setSuited("Y");
                        kwCyphFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, CHANNEL_CODE_KAISEN2, user);
                        if (code.intValue() == BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }
                insert(kwCyphFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    private String createExtId() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(YEAR, 2018);
        calendar.set(MONTH, 5);
        calendar.set(DAY_OF_MONTH, 1);

        int i = random.nextInt(180);

        calendar.add(DAY_OF_MONTH, -i);

        return ymdDateFormat.format(calendar.getTime()) + random.nextInt(10) + random.nextInt(10) + random.nextInt(10);
    }


    /**
     * boolean值转成是、否
     */
    private String booleanToStr(boolean flag) {
        return flag ? "是" : "否";
    }

}
